using System;
using JGSY.CourseManagement.Infrastructure.Security;

namespace JGSY.CourseManagement.Domain.Entities
{
    /// <summary>
    /// 用户实体类，存储用户基本信息
    /// </summary>
    public class User
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string UserId { get; set; } = string.Empty;

        /// <summary>
        /// 用户名（登录用）
        /// </summary>
        public string UserName { get; set; } = string.Empty;

    /// <summary>
    /// 真实姓名
    /// </summary>
    public string RealName { get; set; } = string.Empty;

    /// <summary>
    /// 加密后的密码
    /// </summary>
    public string? Password { get; set; }

    /// <summary>
    /// 是否有效（0:无效, 1:有效）
    /// </summary>
    public int IsValidation { get; set; }

    /// <summary>
    /// 是否允许登录（0:不允许, 1:允许）
    /// </summary>
    public int IsCanLogin { get; set; }

    /// <summary>
    /// 是否教师（0:否, 1:是）
    /// </summary>
    public int IsTeacher { get; set; }

    /// <summary>
    /// 头像URL
    /// </summary>
    public string? Avatar { get; set; }

    /// <summary>
    /// 性别（0:未知, 1:男, 2:女）
    /// </summary>
    public int? Gender { get; set; }

        /// <summary>
        /// 设置密码（自动加密）
        /// </summary>
        /// <param name="plainPassword">明文密码</param> 
        public void SetPassword(string plainPassword)
        {
            if (string.IsNullOrEmpty(plainPassword))
                throw new ArgumentException("密码不能为空", nameof(plainPassword));
            //    // 使用MD5加密密码（实际项目中建议使用更安全的加密方式）
            //    Password = MD5Provider.Encrypt(plainPassword);
            Password = BCrypt.Net.BCrypt.HashPassword(plainPassword);
        }
        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="plainPassword">明文密码</param>
        /// <returns>验证结果</returns>
        public bool VerifyPassword(string plainPassword)
        {
            if (string.IsNullOrEmpty(plainPassword) || string.IsNullOrEmpty(Password))
                return false;
            return BCrypt.Net.BCrypt.Verify(plainPassword, Password);
            //return MD5Provider.Encrypt(plainPassword) == Password;
        }
    }
}